package com.bdqn.controller;

import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysRoleServiceImpl;
import com.bdqn.service.SysUserService;
import com.bdqn.service.SysUserServiceImpl;
import com.bdqn.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * SysUserController
 *
 * @author LILIBO
 * @since 2022-09-08
 */
@WebServlet("/user")
public class SysUserController extends HttpServlet {

    // 引入SysUserService
    private SysUserService sysUserService = new SysUserServiceImpl();

    // 引入SysRoleService
    private SysRoleService sysRoleService = new SysRoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 控制参数
        String opr = request.getParameter("opr");
        switch (opr) {
            case "login": // 用户登录
                login(request, response);
                break;
            case "logout": // 退出系统
                logout(request, response);
                break;
            case "list": // 分页查询用户信息
                list(request, response);
                break;
            default:
                break;
        }
    }

    /**
     * 用户登录
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        SysUser loginUser = sysUserService.login(account, password);
        // 如果登录失败，返回登录页，并提示错误
        if (loginUser == null) {
            request.setAttribute("error", "账号或密码错误");
            request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
            return;
        }
        request.getSession().setAttribute("USER_SESSION", loginUser);
        request.getRequestDispatcher("WEB-INF/jsp/frame.jsp").forward(request, response);
    }

    /**
     * 退出系统
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 清除Session中的数据
        request.getSession().removeAttribute("USER_SESSION");
        // 销毁Session
        request.getSession().invalidate();
        request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
    }

    /**
     * 分页查询用户信息
     */
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取页面查询参数
        String queryRealName = request.getParameter("queryRealName");
        String queryRoleId = request.getParameter("queryRoleId");
        String pageIndex = request.getParameter("pageIndex");

        // 处理角色ID参数
        int roleId = (queryRoleId == null || "".equals(queryRoleId)) ? 0 : Integer.parseInt(queryRoleId);

        // 分页参数（默认查询第1页）
        int pageNo = (pageIndex == null || "".equals(pageIndex)) ? 1 : Integer.parseInt(pageIndex);

        // 获取所有角色信息填充下拉列表
        List<SysRole> roleList = sysRoleService.getAll();

        // 构建分页对象
        Page<SysUser> page = new Page<>();
        // 查询总数据量
        int count = sysUserService.count(queryRealName, roleId);
        page.setTotalCount(count); // 总数据量
        // 先设置总数据量再设置页码
        page.setCurrPageNo(pageNo);
        // 查询分页数据
        List<SysUser> pageList = sysUserService.queryPageByRealNameAndRoleId(queryRealName, roleId, page);
        page.setList(pageList);

        // 数据回显
        request.setAttribute("queryRealName", queryRealName);
        request.setAttribute("queryRoleId", queryRoleId);
        // 填充角色下拉列表
        request.setAttribute("roleList", roleList);
        // 分页数据
        request.setAttribute("page", page);

        // 跳转到列表页面
        request.getRequestDispatcher("WEB-INF/jsp/sysUser/list.jsp").forward(request, response);
    }

}
